//
//  AddressManagerTableViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/25.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "AddressManagerTableViewController.h"
#import "AddressManagerTableViewCell.h"
#import "AddressManagerView.h"
#import "AddAddressUIView.h"
#import "AddAddressViewController.h"

@interface AddressManagerTableViewController ()<addOrDeleteArrayDelegate,seleteAllDelegate,changeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *editBtn;
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)NSMutableArray *array1;
@property (nonatomic,assign)int a;
@property (nonatomic,assign)int b;
- (IBAction)back:(id)sender;
- (IBAction)editClick:(id)sender;
@property (nonatomic,strong)AddressManagerView *foot1;
@property (nonatomic,strong)AddAddressUIView *foot2;
@end

@implementation AddressManagerTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView setEditing:YES animated:YES];
    [self showFoot2];
    [self.tableView setTableFooterView:self.foot2];
    [self showFoot1];
}
-(void)showFoot1
{
    AddressManagerView *foot = [AddressManagerView one];
    foot.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height*0.9, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height*0.1);
    self.foot1 = foot;
    self.foot1.delegate = self;
}
-(void)showFoot2
{
    AddAddressUIView *foot = [AddAddressUIView one];
    foot.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height*0.9, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height*0.1);
    self.foot2 = foot;
    self.foot2.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray arrayWithArray:@[@{@"name":@"王泉",@"iphone":@"158421451",@"address1":@"珠江路新世界大厦B座3912",@"address2":@"江苏省南京市"},@{@"name":@"王泉",@"iphone":@"158421451",@"address1":@"珠江路新世界大厦B座3912",@"address2":@"江苏省南京市"},@{@"name":@"王泉",@"iphone":@"158421451",@"address1":@"珠江路新世界大厦B座3912",@"address2":@"江苏省南京市"},@{@"name":@"王泉",@"iphone":@"158421451",@"address1":@"珠江路新世界大厦B座3912",@"address2":@"江苏省南京市"}]];
    }
    return _array;
}
- (NSMutableArray *)array1
{
    if (!_array1) {
        _array1 = [NSMutableArray array];
    }
    return _array1;
}

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)editClick:(id)sender {
    [self showFooter];
}
- (void)showFooter
{
    if (self.a == 0) {
        [self.tableView setEditing:NO animated:YES];
        [self.editBtn setTitle:@"取消"];
        self.a = 1;
        [self.tableView setTableFooterView:self.foot1];
    }else
    {
        [self.tableView setEditing:YES animated:YES];
        [self.editBtn setTitle:@"编辑"];
        self.a = 0;
        [self.tableView setTableFooterView:self.foot2];
    }
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressManagerTableViewCell *cell = [AddressManagerTableViewCell AddressManagerWithTableView:tableView];
    cell.dic = self.array[indexPath.row];
    
    cell.delegate = self;
    cell.a = (int)indexPath.row;
    if (self.a == 1) {
        cell.selectBtn.hidden = NO;
    }else
    {
        cell.selectBtn.hidden = YES;
    }
    if (self.b == 0) {
        [cell.selectBtn setBackgroundImage:[UIImage imageNamed:@"goodNotSelected.png"] forState:UIControlStateNormal];
        cell.b = 0;
    }else
    {
        [cell.selectBtn setBackgroundImage:[UIImage imageNamed:@"goodSelected.png"] forState:UIControlStateNormal];
        cell.b = 1;
    }
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [[UIScreen mainScreen] bounds].size.height*0.2;
}

//实现可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


//返回处于编辑状态下的按钮 默认为删除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.先判断处于delete状态
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //再删除数据源里的数据
        [self.array removeObjectAtIndex:indexPath.row];
        //更新tableview界面上的数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)addThing:(int)number
{
    NSDictionary *dic = self.array[number];
    [self.array1 addObject:dic];
}

- (void)deleteThing:(int)number
{
    NSDictionary *dic = self.array[number];
    [self.array1 removeObject:dic];
}

- (void)selectAll
{
    [self.array1 addObjectsFromArray:self.array];
    self.b = 1;
    [self.tableView reloadData];
}

- (void)deselectAll
{
    [self.array1 removeAllObjects];
    self.b = 0;
    [self.tableView reloadData];
}

- (void)Result
{
    for ( NSDictionary *dic in self.array1) {
        [self.array removeObject:dic];
    }
    [self deselectAll];
    [self.foot1.selectAllBtn setBackgroundImage:[UIImage imageNamed:@"goodNotSelected.png"] forState:UIControlStateNormal];
    
    [self.tableView reloadData];
    self.a = 0;
    [self showFooter];
    
    
}

- (void)changeViewController
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    AddAddressViewController  *addAddress = [storyBoard instantiateViewControllerWithIdentifier:@"AddAddressViewController"];
    [self presentViewController:addAddress animated:YES completion:nil];
}


@end
